﻿using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace GSQ.Rpt_s
{
    public class printHelper
    {
        PrintDocument Document;
        Font Font;
        StreamReader reader;
        int linePerPage = 0;
        public printHelper(string docName, string textToPrint, string printerName, int Width, int height, string fontName, float fontSize, bool SendToPrint, int linePerPage)
        {
            this.linePerPage = linePerPage;
            this.reader = new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(textToPrint)));
            //Set the font
            this.Font = new Font(fontName, fontSize);

            //Iniciando el printDocument
            this.Document = new PrintDocument();
            this.Document.DocumentName = docName;
            this.Document.DefaultPageSettings.Margins.Left = 0;
            this.Document.DefaultPageSettings.Margins.Right = 0;
            this.Document.DefaultPageSettings.Margins.Top = 0;
            this.Document.DefaultPageSettings.Margins.Bottom = 0;
            this.Document.PrinterSettings.PrinterName = printerName;
            this.Document.DefaultPageSettings.PaperSize = new PaperSize("fuentePapel", Width, height);
            this.Document.PrintPage += new PrintPageEventHandler(Document_PrintPage);
            if (SendToPrint)
                this.Document.Print();
            else
            {
                PrintPreviewDialog pd = new System.Windows.Forms.PrintPreviewDialog();

                pd.Document = this.Document;
                pd.ShowDialog();
            }
        }

        void Document_PrintPage(object sender, PrintPageEventArgs e)
        {
            //Get the Graphics object
            Graphics g = e.Graphics;
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            string line = null;

            //Calculate the lines per page on the basis of the height of the page and the height of the font           
            if (this.linePerPage == 0)
                linesPerPage = float.Parse((e.MarginBounds.Height / this.Font.GetHeight(g)).ToString("N0"));
            else// if (linePerPage > 0)
                linesPerPage = this.linePerPage;
            //Now read lines one by one, using StreamReader
            while (count < linesPerPage - 1 && ((line = reader.ReadLine()) != null))
            {
                //Calculate the starting position
                yPos = topMargin + (count * this.Font.GetHeight(g));
                //Draw text
                g.DrawString(line, this.Font, Brushes.Black, leftMargin, yPos, new StringFormat());
                //Move to next line
                count++;
            }

            //If PrintPageEventArgs has more pages to print
            if (line != null)
            {
                e.HasMorePages = true;
            }
            else
            {
                e.HasMorePages = false;
            }
        }
    }
}
